package com.zj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zj.common.Utility;
import com.zj.dto.TotalInfo;
import com.zj.entity.*;
import com.zj.mapper.*;
import com.zj.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
@Service
@Transactional
public class AdminServiceImpl implements AdminService {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodsCategoryMapper goodsCategoryMapper;
    @Autowired
    AddressMapper addressMapper;
    @Override
    public List<UserInfo> getUserByNumOrNameOrAdd(String queryKey) {
        if (Utility.strIsEmpty(queryKey)){
            return userInfoMapper.selectList(null);
        }else {
            QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("nickname",queryKey).or().like("number",queryKey).or().like("address",queryKey);
            return userInfoMapper.selectList(queryWrapper);
        }
    }

    @Override
    public TotalInfo getTotalInfo() {
        TotalInfo totalInfo = new TotalInfo();
        totalInfo.setUserTotal(userInfoMapper.getUserCount());
        return totalInfo;
    }

    @Override
    public IPage<Goods> selectGoodsByNameOrDescriptionOrUserName(String queryKey,int current,int size) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("goods_name",queryKey).or().like("goods_description",queryKey).or().like("user_name",queryKey);
        IPage page = new Page(current,size); //当前第一页  有两个数据
        goodsMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public IPage<UserInfo> selectUserByNameOrNumOrAddress(String queryKey,int current,int size) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("nickname",queryKey).or().like("address",queryKey).or().like("number",queryKey);
        IPage page = new Page(current,size); //当前第一页  有两个数据
        userInfoMapper.selectPage(page,queryWrapper);
        return page;
    }

    @Override
    public int deleteUserById(String[] id) {
        int row1 = userInfoMapper.deleteBatchIds(Arrays.asList(id));
        int row2 = userMapper.deleteBatchIds(Arrays.asList(id));
        return (row1+row2)/2;
    }

    @Override
    public int deleteGoodsById(String[] id) {
        int row = goodsMapper.deleteBatchIds(Arrays.asList(id));
        return row;
    }

    @Override
    public int updateGoodsById(Goods goods) {
            //商品审核
            Goods tmpGoods =  goodsMapper.selectById(goods.getGoodsId());
            //商品状态必须是 待审核 才允许更新
            //goodsStatus 1待审核 2已上架 3交易中 4已拒绝 5已下架 0已售出
            if (tmpGoods.getGoodsStatus()!=0){
                int row = goodsMapper.updateById(goods);
                return row;
            }else {
                return -1;
            }

    }
    @Override
    public int updateUserById(User user) {
        int row = userMapper.updateById(user);
        return row;
    }

    @Override
    public int updateUserInfoById(UserInfo userInfo) {
        int row = userInfoMapper.updateById(userInfo);
        return row;
    }

    @Override
    public int addGoodsAdmin(Goods goods) {
        UserInfo userInfo = userInfoMapper.selectById(goods.getUserId());
        if (userInfo != null){
            goods.setUuid(Utility.getUUID());
            goods.setCreateTime(Utility.getCurrentDate());
            goods.setGoodsStatus(1);
            goods.setIsDelete(0);
            goods.setUserName(userInfo.getNickname());
            goods.setOwnUserId(userInfo.getUserId());
            int row =  goodsMapper.insert(goods);
            return row;
        }
        return -1;
    }

    @Override
    public int addUserAdmin(User user) {
        int row =  userMapper.insert(user);
        return row;
    }

    @Override
    public int addUserInfoAdmin(UserInfo userInfo) {
        int row =  userInfoMapper.insert(userInfo);
        return row;
    }

    /**
     * 获得商品类别
     *
     * @return {@code List<GoodsCategory>}
     */
    public List<GoodsCategory> getGoodsCategory(){
        return goodsCategoryMapper.selectList(null);
    }

    /**
     * 更新商品类别
     *
     * @param goodsCategory 商品类别
     * @return int
     */
    public int updateGoodsCategory(GoodsCategory goodsCategory){
        int row = goodsCategoryMapper.updateById(goodsCategory);
        return row;
    }

    /**
     * 由cid添加商品类别
     *
     * @return int
     */
    public int addGoodsCategoryByCid(GoodsCategory goodsCategory){
        GoodsCategory gc = new GoodsCategory();
        gc.setName(goodsCategory.getName());
        gc.setCid(goodsCategoryMapper.selectMaxCid()+1);
        gc.setType(goodsCategory.getCid());
        return goodsCategoryMapper.insert(gc);
    }

    /**
     * 删除商品类别id
     *
     * @param id id
     * @return int
     */
    public int deleteGoodsCategoryById(int id) {
        return  goodsCategoryMapper.deleteById(id);
    }

    @Override
    public int addGoodsCategoryOneLevel(GoodsCategory goodsCategory) {
        goodsCategory.setCid(goodsCategoryMapper.selectMaxCid()+1);
        goodsCategory.setType(1);
        System.out.println(goodsCategory);
        return goodsCategoryMapper.insert(goodsCategory);
    }

    public List<Address> getAddress(){
        return addressMapper.selectList(null);
    }
    @Override
    public int updateAddress(Address address) {
        int row = addressMapper.updateById(address);
        return row;
    }

    @Override
    public int addAddressByCid(Address address) {
        Address ad = new Address();
        ad.setName(address.getName());
        ad.setCid(addressMapper.selectMaxCid()+1);
        ad.setType(address.getCid());
        return addressMapper.insert(ad);
    }

    @Override
    public int deleteAddressById(int id) {
        return  addressMapper.deleteById(id);
    }

    @Override
    public int addAddressOneLevel(Address address) {
        address.setCid(addressMapper.selectMaxCid()+1);
        address.setType(1);
        return addressMapper.insert(address);
    }
}
